package com.warski.wonderfires.utils;

import com.warski.wonderfires.utils.date.DateUtils;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by xuhaiping on 2020/8/30.
 */
public class Logger {
    private enum LogType {
        INFO,
        ERROR,
        ;
    }

    public static void info(String format, Object... args) {
        info(String.format(format, args));
    }

    public static void info(String strMsg) {
        long Id = Thread.currentThread().getId();
        recordLog(LogType.INFO, Id, strMsg);
    }

    public static void error(String format, Object... args) {
        error(String.format(format, args));
    }

    public static void error(String strMsg) {
        long Id = Thread.currentThread().getId();
        recordLog(LogType.ERROR, Id, strMsg);
    }

    public static void error(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            t.printStackTrace(pw);
        } finally {
            pw.close();
            error(sw.toString());
        }
    }

    private static void recordLog(LogType type, long threadId, String strMsg){
        String nowTime = DateUtils.getNowTime();

        String logMsg = String.format("%s %s [%05d] -- %s",
                type.toString().toUpperCase(),
                nowTime,
                threadId,
                strMsg
        );

        System.out.println(logMsg);
    }
}
